package com.zxw.article.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zxw.article.mapper.ArticleCategoryMapper;
import com.zxw.article.service.ArticleCategoryService;
import com.zxw.common.constant.Constants;
import com.zxw.common.entity.category.ArticleCategoryEntity;
import com.zxw.common.entity.category.ArticleCategoryListQuery;
import com.zxw.common.entity.category.ArticleCategoryQuery;
import com.zxw.common.entity.category.ArticleCategoryVO;
import com.zxw.common.enums.ResultCode;
import com.zxw.common.exception.BizException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 文章分类表 服务实现类
 * </p>
 *
 * @author zhanxuewei
 * @since 2022-09-19
 */
@Service
public class ArticleCategoryServiceImpl extends ServiceImpl<ArticleCategoryMapper, ArticleCategoryEntity> implements ArticleCategoryService {


    /**
     * 添加
     *
     * @param articleCategoryQuery ArticleCategoryQuery
     * @return 是否成功 true|false
     */
    @Override
    public boolean save(ArticleCategoryQuery articleCategoryQuery) {
        ArticleCategoryEntity articleCategoryEntity = new ArticleCategoryEntity();
        BeanUtils.copyProperties(articleCategoryQuery, articleCategoryEntity);
        articleCategoryEntity.setId(null);
        articleCategoryEntity.setStatus("1");
        articleCategoryEntity.setEnabled(true);
        return super.save(articleCategoryEntity);
    }

    /**
     * 批量添加
     *
     * @param list List<ArticleCategoryQuery>
     * @return 是否成功 true|false
     */
    @Override
    public boolean saveBatch(List<ArticleCategoryQuery> list) {
        List<ArticleCategoryEntity> collect = list.stream().map(item -> {
            ArticleCategoryEntity articleCategoryEntity = new ArticleCategoryEntity();
            BeanUtils.copyProperties(item, articleCategoryEntity);
            articleCategoryEntity.setId(null);
            articleCategoryEntity.setStatus("1");
            articleCategoryEntity.setEnabled(true);
            return articleCategoryEntity;
        }).collect(Collectors.toList());
        return super.saveBatch(collect);
    }

    /**
     * 根据ID集合批量删除
     *
     * @param ids id集合
     * @return 是否成功 true|false
     */
    @Override
    public boolean removeByIds(List<String> ids) {
        return super.removeByIds(ids);
    }

    /**
     * 根据ID修改
     *
     * @param articleCategoryQuery ArticleCategoryQuery
     * @return 是否成功 true|false
     */
    @Override
    public boolean updateById(ArticleCategoryQuery articleCategoryQuery) {
        ArticleCategoryEntity articleCategoryEntity = new ArticleCategoryEntity();
        BeanUtils.copyProperties(articleCategoryQuery, articleCategoryEntity);
        return super.updateById(articleCategoryEntity);
    }

    /**
     * 根据ID查询数据
     *
     * @param id ID
     * @return ArticleCategoryDO
     */
    @Override
    public ArticleCategoryEntity getById(String id) {
        return super.getById(id);
    }

    /**
     * 条件分页查询
     *
     * @param articleCategoryListQuery ArticleCategoryListQuery
     * @return IPage<ArticleCategoryDO>
     */
    @Override
    public IPage<ArticleCategoryEntity> list(ArticleCategoryListQuery articleCategoryListQuery) {
        IPage<ArticleCategoryEntity> pageInfo = new Page<>(articleCategoryListQuery.getCurrent(), articleCategoryListQuery.getSize());
        QueryWrapper<ArticleCategoryEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ArticleCategoryEntity::getStatus, 1)
                .orderByDesc(ArticleCategoryEntity::getCreateTime);
        return super.page(pageInfo, queryWrapper);
    }

    @Override
    public IPage<ArticleCategoryVO> pageList(ArticleCategoryListQuery articleCategoryListQuery) {
        IPage<ArticleCategoryEntity> pageInfo = new Page<>(articleCategoryListQuery.getCurrent(), articleCategoryListQuery.getSize());
        QueryWrapper<ArticleCategoryEntity> queryWrapper = new QueryWrapper<>();
        LambdaQueryWrapper<ArticleCategoryEntity> lambda = queryWrapper.lambda();
        if (StringUtils.isNotBlank(articleCategoryListQuery.getName())) {
            lambda.like(ArticleCategoryEntity::getName, articleCategoryListQuery.getName());
        }
        if (Objects.nonNull(articleCategoryListQuery.getEnabled())) {
            lambda.eq(ArticleCategoryEntity::getEnabled, articleCategoryListQuery.getEnabled());
        }
        lambda.orderByDesc(ArticleCategoryEntity::getCreateTime);
        IPage<ArticleCategoryEntity> page = super.page(pageInfo, queryWrapper);
        return page.convert(articleCategoryEntity -> {
            ArticleCategoryVO articleCategoryVO = new ArticleCategoryVO();
            BeanUtils.copyProperties(articleCategoryEntity, articleCategoryVO);
            return articleCategoryVO;
        });
    }

    @Override
    public ArticleCategoryEntity queryByName(String name) {
        QueryWrapper<ArticleCategoryEntity> queryWrapper = new QueryWrapper<>();
        LambdaQueryWrapper<ArticleCategoryEntity> lambda = queryWrapper.lambda();
        lambda.eq(ArticleCategoryEntity::getName, name);
        return super.getOne(queryWrapper);
    }

    @Override
    public List<ArticleCategoryVO> queryList(ArticleCategoryListQuery articleCategoryListQuery) {
        QueryWrapper<ArticleCategoryEntity> queryWrapper = new QueryWrapper<>();
        LambdaQueryWrapper<ArticleCategoryEntity> lambda = queryWrapper.lambda();
        if (StringUtils.isNotBlank(articleCategoryListQuery.getName())) {
            lambda.like(ArticleCategoryEntity::getName, articleCategoryListQuery.getName());
        }
        if (Objects.nonNull(articleCategoryListQuery.getEnabled())) {
            lambda.eq(ArticleCategoryEntity::getEnabled, articleCategoryListQuery.getEnabled());
        }
        lambda.orderByDesc(ArticleCategoryEntity::getCreateTime);
        List<ArticleCategoryEntity> list = super.list(queryWrapper);
        if(CollUtil.isNotEmpty(list)){
            return list.stream().map(e->{
                ArticleCategoryVO articleCategoryVO = new ArticleCategoryVO();
                BeanUtils.copyProperties(e, articleCategoryVO);
                return articleCategoryVO;
            }).collect(Collectors.toList());
        }
        return Collections.emptyList();
    }

}
